﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GUI_Client_WCF
{
    public partial class ReturnBatteries : Form
    {
        private static ReturnBatteries instance;
        private List<ServiceReference.Booking> list;
        private ServiceReference.IService reference;

        private ReturnBatteries()
        {
            reference = new ServiceReference.ServiceClient();
            list = reference.GetAllBookingsOfCustomer(GUIMain.Customer.customerID).ToList(); 
            
            InitializeComponent();
            ShowTable();
        }

        public static ReturnBatteries Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ReturnBatteries();
                }
                return instance;
            }
        }

        private void ShowTable()
        {
            table.Rows.Clear();
            foreach (var book in list)
            {
                var items = reference.GetBookingItemsFromBooking(book.bookingID).ToList();
                foreach (var item in items)
                {
                    ServiceReference.BatteryItem bi = reference.GetBatteryItem(item.batteryID);
                    ServiceReference.BatteryStation bs = reference.GetBatteryStationByID(bi.stationID);
                    if (bi.state == ServiceReference.BatteryState.taken.ToString())
                        table.Rows.Add(book.bookingID, item.batteryID, bs.name);
                }
            }
            if (table.Rows.Count == 0)
            {
                stationNameLbl.Visible = true;
                returnBtn.Visible = false;
                stationNameLbl.Visible = false;
                stationNameTxtBox.Visible = false;
            }
        }

        private void returnBtn_Click(object sender, EventArgs e)
        {
            if (reference.FindBatteryCenter(stationNameTxtBox.Text.ToString()) != null)
            {
                var batItem = reference.GetBatteryItem(int.Parse(table.CurrentRow.Cells[1].Value.ToString()));
                reference.UpdateBatteryItem(batItem.batteryID, batItem.modelID, stationNameTxtBox.Text.ToString(),
                    ServiceReference.BatteryState.available);
                MessageBox.Show("You have successfully returned the battery", "Succeeded",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                ShowTable();
                stationNameTxtBox.Clear();
            }
            else MessageBox.Show("This battery station does not exist", "Failed",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void back_Click(object sender, EventArgs e)
        {
            GUIBookings bookings = GUIBookings.Instance;
            bookings.Show();

            this.Close();
        }
    }
}
